package com.android.bookgarden.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.HomeDataBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.tabfragment.JiaoshiFragment;
import com.android.bookgarden.tabfragment.KeTangFragment;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;
import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpHeaders;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * 课堂详情
 * Create by zouqh
 * on 2018/12/3
 **/
public class ClassroomDetailsActivity extends BaseActivity implements View.OnClickListener,RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.mbgabanner)
    BGABanner mbgabanner;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.locationCity)
    TextView locationCity;
    @BindView(R.id.onlineNum)
    TextView onlineNum;
    @BindView(R.id.kcjsText)
    TextView kcjsText;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.jslbText)
    TextView jslbText;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.fragment)
    FrameLayout fragment;
    @BindView(R.id.shareIcon)
    ImageView shareIcon;
    List<String> images;

    private FragmentManager fm;
    private KeTangFragment keTangFragment;
    private JiaoshiFragment jiaoshiFragment;
    private HomeDataBean bean;
    private RealtConstract.Presenter presenter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_classroom_details;
    }

    @Override
    protected void initView() {
        bean = (HomeDataBean) getIntent().getSerializableExtra("bean");
        fm = getSupportFragmentManager();

        jiaoshiFragment = JiaoshiFragment.newInstance(bean.getId());
        presenter = new BasePersenter(this,this);
        titleText.setText("课堂详情");
        close.setOnClickListener(this);
        shareIcon.setOnClickListener(this);
        ritghIcon.setOnClickListener(this);
        kcjsText.setOnClickListener(this);
        jslbText.setOnClickListener(this);
        kcjsText.setTextColor(getResources().getColor(R.color.grad_text));
        if (null!=bean){
            System.out.println(bean);
            keTangFragment = KeTangFragment.newInstance( bean.getCoursedesc());
            locationCity.setText(""+bean.getDetailaddr());
            name.setText(""+bean.getBusinessname());
            onlineNum.setText(""+bean.getLearnnum());
            if (bean.isIscollect()){
                ritghIcon.setImageResource(R.mipmap.love_on);
            }else{
                ritghIcon.setImageResource(R.mipmap.love_off);
            }
        }
//        images = new ArrayList();
//        images.add("http://image14.m1905.cn/uploadfile/2018/0907/thumb_1_1380_460_20180907013518839623.jpg");
//        images.add("http://image14.m1905.cn/uploadfile/2018/0906/thumb_1_1380_460_20180906040153529630.jpg");
//        images.add("http://image13.m1905.cn/uploadfile/2018/0907/thumb_1_1380_460_20180907114844929630.jpg");
////设置图片集合
//        mbgabanner.setData(images,null);
        if (null!=bean&&null!=bean.getImgurls()){
            //设置图片集合
            mbgabanner.setData(bean.getImgurls(),null);
        }

        //banner设置方法全部调用完毕时最后调用
        mbgabanner.setAdapter(new BGABanner.Adapter<ImageView, String> (){

            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(ClassroomDetailsActivity.this)
                        .load(model)
                        .placeholder(R.mipmap.pic)
                        .error(R.mipmap.ic_launcher)
                        .dontAnimate()
                        .centerCrop()
                        .into(itemView);
            }
        });
        setSelected(0);
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_icon:
                if (!bean.isIscollect()){
                    addColleciton();
                }

                break;

            case R.id.shareIcon:
                startActivity(ShareActivity.class);
                break;

            case R.id.kcjsText:
                kcjsText.setTextColor(getResources().getColor(R.color.blak));
                jslbText.setTextColor(getResources().getColor(R.color.grad_text));
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.INVISIBLE);
                setSelected(0);
                break;
            case R.id.jslbText:
                kcjsText.setTextColor(getResources().getColor(R.color.grad_text));
                jslbText.setTextColor(getResources().getColor(R.color.blak));
                view2.setVisibility(View.VISIBLE);
                view1.setVisibility(View.INVISIBLE);
                setSelected(1);
                break;
        }
    }

    private void setSelected(int i) {

        switch (i) {
            case 0:
                showFragment(keTangFragment);
                break;


            case 1:
                showFragment(jiaoshiFragment);
                break;

            default:

                break;

        }

    }

    private Fragment currentFragment;
    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private void showFragment(Fragment fg) {

        FragmentTransaction transaction = fm.beginTransaction();

        //如果之前没有添加过
        if (!fg.isAdded()) {
            if (null != currentFragment) {
                transaction
                        .hide(currentFragment)
                        .add(R.id.fragment, fg);
            } else {
                transaction
                        .add(R.id.fragment, fg);
            }

        } else {
            transaction
                    .hide(currentFragment)
                    .show(fg);
        }

        //全局变量，记录当前显示的fragment
        currentFragment = fg;

        transaction.commit();

    }

    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }

        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {
                case 1001:
                    try {
                        LoadingDialog.hideProgress();
                        DataBean bean = Utlis.getDataBean(data);
                        showToast(""+bean.getMessage());
                        if (bean.getCode() == 200) {
                            ClassroomDetailsActivity.this.bean.setIscollect(true);
                            ritghIcon.setImageResource(R.mipmap.love_on);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

            }
        }
    };

    private void addColleciton() {
        LoadingDialog.showMassge(this,"收藏中...");
        HttpHeaders headers = new HttpHeaders();
        headers.put("token", MainActivity.mainActivity.token);

        presenter.loadDataHeader(null, "", Apis.COLLECTION +bean.getId() +"/1", 1001, headers, HttpType.POST);
    }
}
